package com.ybf.api.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.ybf.api.common.ResultResponse;
import com.ybf.api.entity.PageRequest;
import com.ybf.api.entity.User;
import com.ybf.api.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.ybf.api.utils.Utils.mapToObject;

@Controller
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @Operation(summary = "登录")
    @PostMapping("login")
    @ResponseBody
    public ResultResponse login(@RequestBody @Valid User user) {
        User userInfo = userService.login(user.getUsername(), user.getPassword());
        if (userInfo != null) {
            StpUtil.login(userInfo.getId());
            StpUtil.getSession().set("user", userInfo);
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            userInfo.setToken(tokenInfo.tokenValue);
            userInfo.setTokenTimeout(tokenInfo.tokenTimeout);
            return ResultResponse.ok(userInfo);
        }
        return ResultResponse.error("用户名或者密码错误");
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    @ResponseBody
    public ResultResponse logout() {
        return userService.logout() ? ResultResponse.ok(true) : ResultResponse.error(false);
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    @ResponseBody
    public ResultResponse list(
            @RequestBody Map reqBody
    ) {
        User user = (User) mapToObject(User.class, reqBody);
        PageRequest pageRequest = (PageRequest) mapToObject(PageRequest.class, reqBody);
        return ResultResponse.ok(userService.list(user, pageRequest));
    }

    @Operation(summary = "获取用户详情")
    @GetMapping("/info")
    @ResponseBody
    public ResultResponse info(
            @RequestBody(required = true) Map req
    ) {
        User user = userService.info((Integer) req.get("id"));
        return user != null ? ResultResponse.ok(user) : ResultResponse.error("error");
    }

    @Operation(summary = "删除用户详情")
    @GetMapping("/delete")
    @ResponseBody
    public ResultResponse delete(@RequestBody Map req) {
        Boolean status = userService.delete((Integer) req.get("id"));
        return status != null ? ResultResponse.ok(true) : ResultResponse.error(false);
    }

    @Operation(summary = "编辑用户详情", description = "不传用户id就是新增，传了就是编辑")
    @PostMapping("/edit")
    @ResponseBody
    public ResultResponse edit(@RequestBody User user) {
        Integer status = userService.edit(user);
        return status != null ? ResultResponse.ok(status) : ResultResponse.error(false);
    }
}
